<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" :close-on-click-modal="false" append-to-body modal-append-to-body>
		<el-form ref="form" :model="form" :rules="rules">
			<el-form-item label="题库分类" :label-width="formLabelWidth" prop="qbtypeId">
				<fortress-select v-model="form.qbtypeId" :dictList="qbtypeIdList" placeholder="请选择题库分类" clearable></fortress-select>
			</el-form-item>
			<el-form-item label="名称" :label-width="formLabelWidth" prop="qbName">
				<el-input v-model="form.qbName" autocomplete="off" placeholder="请输入名称" maxlength="100" show-word-limit></el-input>
			</el-form-item>
			<el-form-item label="开放学习开始时间" prop="openStartTime">
				<el-date-picker
					format="yyyy-MM-dd HH:mm:ss"
					value-format="yyyy-MM-dd HH:mm:ss"
					type="datetime"
					placeholder="开放学习开始时间"
					v-model="form.openStartTime"
				></el-date-picker>
			</el-form-item>
			<el-form-item label="开放学习结束时间" prop="openEndTime">
				<el-date-picker
					format="yyyy-MM-dd HH:mm:ss"
					value-format="yyyy-MM-dd HH:mm:ss"
					type="datetime"
					placeholder="开放学习结束时间"
					v-model="form.openEndTime"
				></el-date-picker>
			</el-form-item>
			<el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
				<template #label>
					<span style="margin-right: 7px;">排序</span>
					<el-tooltip class="item" effect="dark" content="数字越大越靠前" placement="top">
						<i class="el-icon-info"></i>
					</el-tooltip>
				</template>
				<el-input-number v-model="form.sort"></el-input-number>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
			<el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
		</div>
	</el-dialog>
</template>
<script>
import { studyquestionbankInfo, studyquestionbankSave, studyquestionbankEdit } from "@/api/study/exam/studyquestionbank.js"
import { enumList, urlList } from "@/api/public.api.js"

export default {
	props: {
		parentId: {
			type: String,
			default: "",
		},
	},
	data() {
		return {
			visible: false,
			form: {
				qbtypeId: "",
				qbName: "",
				sort: "",
				openStartTime: "",
				openEndTime: "",
			},
			formLabelWidth: "120px",
			rules: {
				qbtypeId: [{ required: true, message: "请输入题库分类", trigger: "blur" }],
				qbName: [{ required: true, message: "请输入名称", trigger: "blur" }],
			},
			YesNoList: [],
			qbtypeIdList: [],
		}
	},
	created() {},
	methods: {
		queryEnumList() {
			this.YesNoList = []
			enumList("YesNo", "").then(resp => {
				if (resp.code == 0) {
					this.YesNoList = resp.data
				}
			})
		},
		queryUrlList() {
			this.qbtypeIdList = []
			urlList("/study/exam/studyQuestionbankType/list").then(resp => {
				if (resp.code == 0) {
					if (resp.data) {
						resp.data.forEach(item => {
							this.qbtypeIdList.push({ code: item.id, name: item.name })
						})
					}
				}
			})
		},
		init(id) {
			this.resetForm()
			this.queryEnumList()
			this.queryUrlList()
			//修改
			if (id) {
				studyquestionbankInfo(id).then(resp => {
					if (resp.code == 0) {
						this.$nextTick(() => {
							this.form = {
								...this.form,
								...resp.data,
							}
						})
					} else {
						this.$msg({
							message: resp.msg,
							type: "error",
						})
					}
				})
			} else {
				this.$nextTick(() => {
					this.form.qbtypeId = this.parentId
				})
			}
			this.visible = true
		},
		//提交表单数据
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					//修改
					if (this.form.id) {
						studyquestionbankEdit(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
						//新增
					} else {
						studyquestionbankSave(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
					}
				} else {
					console.log("error submit!!")
					return false
				}
			})
		},
		resetForm() {
			this.form = {
				qbtypeId: "",
				qbName: "",
				sort: "",
				openStartTime: "",
				openEndTime: "",
			}
			this.$nextTick(() => {
				if (this.$refs["form"]) {
					this.$refs["form"].resetFields()
				}
			})
		},
	},
}
</script>
<style scoped>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409eff;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
	height: auto;
	max-height: 274px;
	padding: 7px;
	overflow: hidden;
	overflow-y: auto;
	font-weight: normal;
}
</style>
